package com.dykj.fireFighting.web.outdoorDevice;

import java.util.*;

import com.dykj.fireFighting.api.socialUnit.outdoorDevice.dto.OutdoorDeviceDto;
import com.dykj.fireFighting.api.socialUnit.outdoorDevice.entity.OutdoorDevice;
import com.dykj.fireFighting.api.socialUnit.outdoorDevice.service.IOutdoorDeviceService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;



/**
 * 接口
 *
 * @Author zcf
 * @Date  2021-08-18 10:06:42
 */
@Slf4j
@Api(value = "接口", tags = {"接口"})
@RestController
@RequestMapping("outdoorDevice")
public class OutdoorDeviceController {

    @DubboReference
    private IOutdoorDeviceService outdoorDeviceService;
    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }
    /**
     * 获取用户id
     * @return String
     */
    public Long getId() {
        try {
            return ShiroUtil.getUser().getId();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 列表
     *
     * @return
     */
    @AutoLog(value = "-分页列表查询")
    @ApiOperation(value = "分页列表", notes = "分页列表")
    @GetMapping("/list")
    @RequiresPermissions("outdoorDevice/list")
    public CommonResult<PageVo<OutdoorDevice>> treeGrid(OutdoorDeviceDto commonDto) {
        if (StringUtils.isBlank(getEnterpriseId())) {
            throw new BaseException("请登录系统");
        } else {
            commonDto.setUnitId(getEnterpriseId());
        }
        PageVo<OutdoorDevice> page = outdoorDeviceService.queryListPage(commonDto);
        return CommonResult.success(page);
    }

    @AutoLog(value = "列表查询")
    @ApiOperation(value = "分页列表", notes = "分页列表")
    @GetMapping("/selectList")
    //@RequiresPermissions("outdoorDevice/list")
    public CommonResult<List<OutdoorDevice>> queryList(OutdoorDeviceDto commonDto) {
        if (StringUtils.isBlank(getEnterpriseId())) {
            throw new BaseException("请登录系统");
        } else {
            commonDto.setUnitId(getEnterpriseId());
        }
        List<OutdoorDevice> page = outdoorDeviceService.queryList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取信息
     */
    @AutoLog(value = "-获取信息")
    @ApiOperation(value = "获取信息", notes = "获取信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("outdoorDevice/info")
    public CommonResult<OutdoorDevice> info(@PathVariable("id") String id){
        OutdoorDevice outdoorDevice = outdoorDeviceService.getById(id);
        return CommonResult.success(outdoorDevice);
    }

    /**
     * 保存信息
     */
    @AutoLog(value = "-保存信息")
    @ApiOperation(value = "保存信息", notes = "保存信息")
    @PostMapping("/save")
    @RequiresPermissions("outdoorDevice/save")
    public CommonResult save(@Valid @RequestBody OutdoorDevice outdoorDevice) {
        try {
            outdoorDevice.setCreateTime(new Date());
            outdoorDevice.setId(UUIDUtils.get32UUID());
            if (StringUtils.isBlank(getEnterpriseId())) {
                throw new BaseException("请登录系统");
            } else {
                outdoorDevice.setUnitId(getEnterpriseId());
                outdoorDevice.setCreateBy(Long.toString(getId()));
            }
                outdoorDeviceService.save(outdoorDevice);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改信息
     */
    @AutoLog(value = "-修改信息")
    @ApiOperation(value = "修改信息", notes = "修改信息")
    @PostMapping("/update")
    @RequiresPermissions("outdoorDevice/update")
    public CommonResult update(@Valid @RequestBody OutdoorDevice outdoorDevice) {
        try {
            outdoorDevice.setUpdateTime(new Date());
            if (StringUtils.isBlank(getEnterpriseId())) {
                throw new BaseException("请登录系统");
            } else {
                outdoorDevice.setUpdateBy(Long.toString(getId()));
            }
                outdoorDeviceService.updateById(outdoorDevice);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除信息
     */
    @AutoLog(value = "-删除信息")
    @ApiOperation(value = "删除信息", notes = "删除信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("outdoorDevice/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                outdoorDeviceService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
